using System.Collections.Generic;

namespace Core
{
    public class StateMachineComponent : Entity
    {
        private StateType curStateName = StateType.None;
        private Dictionary<StateType, IState> states = new Dictionary<StateType, IState>();

        public StateType State => curStateName;

        public void AddState(StateType stateName, IState state)
        {
            if (stateName == StateType.None)
                return;
            
            states[stateName] = state;
        }

        public bool ChangedState(StateType changeStateName)
        {
            if (curStateName == changeStateName)
                return false;

            if(states.TryGetValue(changeStateName,out var newState))
            {
                if (states.TryGetValue(curStateName, out var curState))
                {
                    curState?.Exit();
                }

                newState?.Enter();
                curStateName = changeStateName;
                return true;
            }

            return false;
        }
    }
}